package com.aijk.ehm.service;

import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.dao.mybatis.SemStepMapper;
import com.aijk.ehm.model.SemStep;
import com.aijk.ehm.model.SemStepExample;
import com.aijk.ehm.model.qo.SemStepQo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 步数
 */
@Service
public class SemStepService {
    private static Logger logger = LoggerFactory.getLogger(SemStepService.class);

    @Autowired
    private SemStepMapper mapper;

    public SemStepExample getExample(SemStepQo qo) {
        SemStepExample example = new SemStepExample();
        SemStepExample.Criteria criteria = example.createCriteria();
        if (qo != null) {
            if (Utils.isNotEmpty(qo.getSortway())) example.setOrderByClause(qo.getSortway());
            if (qo.getStartdate()!= null) criteria.andMeterdateGreaterThanOrEqualTo(qo.getStartdate());
            if (qo.getEnddate()!= null) criteria.andMeterdateLessThanOrEqualTo(qo.getEnddate());
            if (qo.getResidentId()!= null) criteria.andResidentIdEqualTo(qo.getResidentId());
            if (qo.getUid()!= null) criteria.andUidEqualTo(qo.getUid());
            if (qo.getIdcard()!= null) criteria.andIdcardEqualTo(qo.getIdcard());
            if (qo.getStepNum()!= null) criteria.andStepNumGreaterThanOrEqualTo(qo.getStepNum());
            if (qo.getDevicesn()!= null && qo.getDevicesn()!="") criteria.andDevicesnEqualTo(qo.getDevicesn());
            if (qo.getCompanyId()!=null && Utils.toInteger(qo.getCompanyId()) != 0) criteria.andCompanyIdEqualTo(qo.getCompanyId());
            if (qo.getResidentId()!=null && Utils.toInteger(qo.getResidentId()) != 0) criteria.andResidentIdEqualTo(qo.getResidentId());

        }
        return example;
    }

    public int count(SemStepQo qo) {
        return mapper.countByExample(getExample(qo));
    }

    public int save(SemStep bean) {
        return mapper.insertSelective(bean);
    }

    public int delete(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    public int delete(SemStepQo qo){
        return mapper.deleteByExample(getExample(qo));
    }

    public int update(SemStep bean) {
        return mapper.updateByPrimaryKeySelective(bean);
    }

    public SemStep findOne(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<SemStep> find(SemStepQo qo) {
        return mapper.selectByExample(getExample(qo));
    }

    public PageInfo<SemStep> page(PagingRequest pagingRequest, SemStepQo qo) {
        PageHelper.startPage(pagingRequest.getPageNum() , pagingRequest.getPageSize());
        return new PageInfo<SemStep>(mapper.selectByExample(getExample(qo)));
    }
}